<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="渠道商" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入渠道商"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />

      </el-form-item>
      <el-form-item label="班次" prop="timeShiftName">
        <el-input
          v-model="queryParams.timeShiftName"
          placeholder="请输入班次"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="班组" prop="groupName">
        <el-input
          v-model="queryParams.groupName"
          placeholder="请输入班组"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="工作时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.workTime"
          align="right"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="info"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >保存</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="info"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
     <el-table v-loading="loading" :data="tableData"
               :span-method="objectSpanMethod"
               border>
      <el-table-column type="selection" width="150" align="center"/>
      <el-table-column label="渠道商" prop="companyName"  align="center" width="150"/>
      <el-table-column label="班次" prop="timeShiftName" align="center" :show-overflow-tooltip="true" width="200"/>
       <el-table-column label="性别" prop="sex" align="center" :show-overflow-tooltip="true" width="200">
         <template slot-scope="scope">
           <div v-if="scope.row.sex == 1">男</div>
           <div v-else-if="scope.row.sex == 2">女</div>
         </template>
       </el-table-column>
      <el-table-column label="需求数量" align="center" prop="needNumber" width="150"/>
      <el-table-column label="合计"  prop="needNumberTotal" align="center" width="150"/>
       <el-table-column label="实际数量"  prop="actNumber" align="center" width="150"/>
      <el-table-column label="合计" align="center" prop="actNumberTotal"  width="150"/>
    </el-table>

  </div>
</template>

<script>
import {listAttendance,savaAttendanceInfos,exportAttendance} from "@/api/job/attendance/attendanceApi";
import { getToken } from "@/utils/auth";
export default {
  name: "attendanceGroup",
  created() {
   this.getTime ();
    this.getList();

  },
  data() {
    return {
      tableData: [{
        companyName: '王小虎',
        timeShiftName: '234',
        sex: 1,
        needNumber: 10,
        needNumberTotal:0,
        actNumber:0,
        actNumberTotal:100
      }, {
        companyName: '王小虎',
        timeShiftName: '234',
        sex: 1,
        needNumber: 10,
        needNumberTotal:0,
        actNumber:0,
        actNumberTotal:100
      }, {
        companyName: '王小虎',
        timeShiftName: '234',
        sex: 1,
        needNumber: 10,
        needNumberTotal:0,
        actNumber:0,
        actNumberTotal:100
      }, {
        companyName: '王小虎',
        timeShiftName: '234',
        sex: 1,
        needNumber: 10,
        needNumberTotal:0,
        actNumber:0,
        actNumberTotal:100
      }, {
        companyName: '王小虎',
        timeShiftName: '234',
        sex: 1,
        needNumber: 10,
        needNumberTotal:0,
        actNumber:0,
        actNumberTotal:100
      }],
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24);
            picker.$emit('pick', date);
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', date);
          }
        }]
      },
      savaDatas:[],
      signUpLoading:true,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      attendanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      menuExpand: false,
      menuNodeAll: false,
      deptExpand: true,
      deptNodeAll: false,
      // 日期范围
      dateRange: [],
      signUp:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        //渠道名称
        companyName: undefined,
        timeShiftName:undefined,
        groupName:undefined,
        //联系人
        workerName: undefined,
        workTime:undefined
      },
      timeShiftList:[],
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
    };
  },
  methods: {
    getTime () {
      let _this = this;
      let date = new Date();
      let y = date.getFullYear();
      let m = (date.getMonth() + 1) < 10 ? ('0' + (date.getMonth() + 1)) : (date.getMonth() + 1);
      let d = date.getDate() < 10 ? ('0' + date.getDate()) : date.getDate();

      let start_month = y + '-' + '01';   //开始月份：本年第一月份
      let end_month = `${y}-${m}`;        //结束月份： 本年当前月份

      let start_day = `${y}-${m}-${d}`;       //开始日期： 本月第一天
      let end_day = `${y}-${m}-${d}`;     //结束日期： 本月今天

      //_this.aa = [start_month, end_month];   //默认月份区间
      _this.queryParams.workTime  = start_day;        //默认日期区间
    },
    /** 获取员工信息列表 */
    getList() {
      this.loading = true;
      listAttendance(this.addDateRange(this.queryParams, this.dateRange)).then(
        response => {
          this.attendanceList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 表单重置
    reset() {
        this.form = {
          id:undefined,
          groupName: undefined,
          workTime: undefined
        };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleExport(){
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有员工数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportAttendance(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    },
    handleAdd() {
      if (this.savaDatas.length == 0 ) {
        this.$alert("请选择需要保存的数据", "保存错误");
      }else {
        savaAttendanceInfos(this.savaDatas).then(res=>{
          this.msgSuccess( "数据保存成功");
        }).catch(()=>{
          this.msgError("保存失败");
        })
      }
    },
    /**
     * 合并表格
     * @param row
     * @param column
     * @param rowIndex
     * @param columnIndex
     */
    objectSpanMethod({ row, column, rowIndex, columnIndex }){
      //第一列
      if (columnIndex === 0) {
        if (rowIndex % 2 !== 0) {
          return {
            rowspan: 2,
            colspan: 1
          };
        } else {
          return {
            rowspan: 0,
            colspan: 0
          };
        }
      }
      //第二例
      if (columnIndex === 1) {

      }

    }
  }
};
</script>
